﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Samples.KMoore.WPFSamples.DateControls;
using StockholmsStad.Windows.UI.Infrastructure.ValidationRules;
using Microsoft.Windows.Controls;

namespace StockholmsStad.Windows.UI.Infrastructure.Controls
{
    /// <summary>
    /// Interaction logic for ImageButton2.xaml
    /// </summary>
    public partial class DateTimePicker
    {
        public event EventHandler SelectedDateChanged;

        public static readonly DependencyProperty SelectedDateProperty =
            DependencyProperty.Register("SelectedDate", typeof(DateTime?), typeof(DateTimePicker), new UIPropertyMetadata(null));

        public DateTimePicker()
        {
            InitializeComponent();

            calendarControl.SelectedDatesChanged += calendarControl_SelectedDatesChanged;

            tbText.TextChanged += tbText_TextChanged;
            tbText.LostFocus += tbText_LostFocus;
        }

        public DateTime? SelectedDate
        {
            get { return (DateTime?)GetValue(SelectedDateProperty); }
            set { SetValue(SelectedDateProperty, value); }
        }

        public Calendar PopUpCalendar
        {
            get { return calendarControl; }
            set { calendarControl = value; }
        }

        void tbText_TextChanged(object sender, TextChangedEventArgs e)
        {
            DateTime tryDate;

            //bool isDate = DateTime.TryParse(tbText.Text, out tryDate);
            if (DateTime.TryParse(tbText.Text, out tryDate))
            {
                calendarControl.SelectedDate = tryDate;
            }

            //var text = tbText.Text;
            //if (isDate && tryDate.ToShortDateString() != tbText.Text)
            //    tbText.Text = tryDate.ToShortDateString();
        }

        void tbText_LostFocus(object sender, RoutedEventArgs e)
        {
            tbText.Text = tbText.Text.Trim();

            DateTime tryDate;
            if (tbText.Text.Length >= 8)
            {
                bool validDate = true;

                if (tbText.Text.Length == 8)
                {
                    validDate = DateTimeFormat.IsValidDate(tbText.Text);
                    tbText.Text = tbText.Text.Insert(6, "-").Insert(4, "-");
                }
                else if(tbText.Text.Length == 10)
                {
                    if (!DateTime.TryParse(tbText.Text, out tryDate))
                        validDate = false;
                }

                if(!validDate)
                {
                    calendarControl.SelectedDate = null;
                    MessageBox.Show("Datumet är felaktigt. Ange datum enligt formatet 'åååå-mm-dd'.");
                }
            }
        }

        private void openPopupCalendar_Click(object sender, MouseButtonEventArgs e)
        {
            popupCalendar.IsOpen = true;
        }

        private void calendarControl_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
        {
            if (calendarControl.SelectedDate.HasValue)
            {
                tbText.Text = calendarControl.SelectedDate.Value == DateTime.MinValue ?
                    string.Empty : calendarControl.SelectedDate.Value.ToString("yyyy-MM-dd");
            }
            else
            {
                tbText.Text = string.Empty;
            }
            tbText.Focus();
            if (SelectedDateChanged != null)
                SelectedDateChanged(this, new EventArgs());

            popupCalendar.IsOpen = false;
        }

    }
}
